rm(list=ls())
library(spatstat)
library(plyr)
library(lattice)

source("set_up.R", encoding = "UTF-8")

### Figure 2
load("ppmoutcome_r5.RData")
load("ppmoutcome_r6.RData")
load("ppmoutcome_r7.RData")

r5 <- ppm_ceof2(ModelResults = list(r5_r50, r5_r80),
                subvar = TRUE, 
                vars = c("internet_freq.im"),
                year = "2013") 

r6 <- ppm_ceof2(ModelResults = list(r6_r50, r6_r80),
                            subvar = TRUE, 
                            vars = c("internet_freq.im"),
               year = "2014") 




r7 <- ppm_ceof2(ModelResults = list(r7_r50, r7_r80),
                subvar = TRUE, 
                vars = c("internet_freq.im"),
                year = "2017") 



df <- bind_rows(r5, r6)



p = ggplot(df, aes(colour = Model.Name)) + 
  geom_hline(yintercept = 0, lty = 2) +
  geom_linerange(aes(x = year, ymin = Low90CI,
                     ymax = High90CI),
                 lwd = 2, position = position_dodge(width = 1/2)) +
  geom_pointrange(aes(x = year, y = Coefficients, ymin = Low95CI,
                      ymax = High95CI, shape = Model.Name),
                  lwd = 1, position = position_dodge(width = 1/2),
                  fill = "WHITE") +
  theme_bw() + xlab("") + ylab("标准化回归系数") + 
  theme(legend.position="bottom",
        legend.title=element_blank(),
        legend.text = element_text(colour="blue", size=12, 
                                   face="bold"),
        axis.text= element_text(size=12),
        text = element_text(size=14,family ='STSong'),
        plot.title = element_text(hjust = .5, size = 12, face = "bold"))


m1 <- p +ggtitle("互联网对暴力冲突的动态影响")+
  scale_colour_manual(values = c("black", "blue"),
                      labels = c("50千米半径","80千米半径"))+
  scale_shape_manual(values = c(17,22), labels = c("50千米半径","80千米半径"))+
  scale_x_discrete(labels = c("2013（第五轮）","2014（第六轮）")) 

## Figure 2-a
ggsave("figures2023/fig_internet.png", dpi = 600, width = 8, height = 6, units = "in")


### 2017

df2 <- bind_rows(r5, r7)



p1 = ggplot(df2, aes(colour = Model.Name)) + 
  geom_hline(yintercept = 0, lty = 2) +
  geom_linerange(aes(x = year, ymin = Low90CI,
                     ymax = High90CI),
                 lwd = 2, position = position_dodge(width = 1/2)) +
  geom_pointrange(aes(x = year, y = Coefficients, ymin = Low95CI,
                      ymax = High95CI, shape = Model.Name),
                  lwd = 1, position = position_dodge(width = 1/2),
                  fill = "WHITE") +
  theme_bw() + xlab("") + ylab("标准化回归系数") + 
  theme(legend.position="bottom",
        legend.title=element_blank(),
        legend.text = element_text(colour="blue", size=12, 
                                   face="bold"),
        axis.text= element_text(size=12),
        text = element_text(size=14,family ='STSong'),
        plot.title = element_text(hjust = .5, size = 12, face = "bold"))


p1 +ggtitle("互联网对暴力冲突的动态影响")+
  scale_colour_manual(values = c("black", "blue"),
                      labels = c("50千米半径","80千米半径"))+
  scale_shape_manual(values = c(17,22), labels = c("50千米半径","80千米半径"))+
  scale_x_discrete(labels = c("2013（第五轮）","2017（第七轮）")) 

## Figure 2-b
ggsave("figures2023/fig_internet2.png", dpi = 600, width = 8, height = 6, units = "in")


############### Table 3

summary(r5_r50)
round(coef(r5_r50),digits = 4)
round(sqrt(diag(vcov.ppm(r5_r50))), digits = 4)
AIC(r5_r50)

summary(r5_r80)
round(coef(r5_r80),digits = 4)
round(sqrt(diag(vcov.ppm(r5_r80))), digits = 4)
AIC(r5_r80)


summary(r6_r50)
round(coef(r6_r50),digits = 4)
round(sqrt(diag(vcov.ppm(r6_r50))), digits = 4)
AIC(r6_r50)

summary(r6_r80)
round(coef(r6_r80),digits = 4)
round(sqrt(diag(vcov.ppm(r6_r80))), digits = 4)
AIC(r6_r80)



summary(r7_r50)
round(coef(r7_r50),digits = 4)
round(sqrt(diag(vcov.ppm(r7_r50))), digits = 4)
AIC(r7_r50)

summary(r7_r80)
round(coef(r7_r80),digits = 4)
round(sqrt(diag(vcov.ppm(r7_r80))), digits = 4)
AIC(r7_r80)
